#include "GhostObject.h"

GhostObject::GhostObject()
{}

void GhostObject::initalize(EGhostColor color, Vec2 position, float scale)
{
	setAnchorPoint(Vec2(0.5f, 0.5f));
    this->setPosition(position);
	m_scale = scale;
	m_color = color;

	m_body = Button::create();
    m_body->setPosition(Vec2::ZERO);
	m_body->setScale(m_scale);

	initBody();
	initComponent();

	this->addChild(m_body, EGhostZOrder::kGhostZOrder_Body);
	this->addChild(m_eyes, EGhostZOrder::kGhostZOrder_Eyes);
	this->addChild(m_mouth, EGhostZOrder::kGhostZOrder_Mouth);
	this->addChild(m_leftHand, EGhostZOrder::kGhostZOrder_Hand);
	this->addChild(m_rightHand, EGhostZOrder::kGhostZOrder_Hand);
}

void GhostObject::initBody()
{
	std::string s;
	switch (m_color)
	{
	case kGhostColor_Red: s = "ghost_red.png"; break;
	case kGhostColor_Blue: s = "ghost_blue.png"; break;
	case kGhostColor_Purple: s = "ghost_purple.png"; break;
	}

	const char* name = s.c_str();
	m_body->loadTextures(name, name);
}

void GhostObject::initComponent()
{
	m_eyes = Sprite::create("ghost_eye.png");
	m_mouth = Sprite::create("ghost_mouth.png");
	m_leftHand = Sprite::create("left_hand.png");
	m_rightHand = Sprite::create("right_hand.png");

	m_eyes->setScale(m_scale);
	m_mouth->setScale(m_scale);
	m_leftHand->setScale(m_scale);
	m_rightHand->setScale(m_scale);

	m_eyes->setPosition(Vec2(m_body->getPosition().x, m_body->getPosition().y));
	m_mouth->setPosition(Vec2(m_eyes->getPosition().x, m_eyes->getPosition().y - m_eyes->getContentSize().height*m_scale));
	m_leftHand->setPosition(m_body->getPosition().x + m_leftHand->getContentSize().width*m_scale / 3 * 2, m_body->getPosition().y);
	m_rightHand->setPosition(m_body->getPosition().x - m_rightHand->getContentSize().width*m_scale / 3 * 2, m_body->getPosition().y);
}

void GhostObject::setState(EGhostState state)
{
	if (m_state == state)
		return;

	m_state = state;
	switch (state)
	{
	case kGhostState_Default: runStateDefault(); break;
	case kGhostState_Stay: runStateStay(); break;
	case kGhostState_Yeah: runStateYeah(); break;
	}
}

EGhostState GhostObject::getState()
{
	return m_state;
}

void GhostObject::setComponentVisible(EGhostComponent component, bool visible)
{
	switch (component)
	{
	case kGhostComponent_Body:
		m_body->setVisible(visible);
		break;
	case kGhostComponent_Eyes:
		m_eyes->setVisible(visible);
		break;
	case kGhostComponent_Mouth:
		m_mouth->setVisible(visible);
		break;
	case kGhostComponent_LeftHand:
		m_leftHand->setVisible(visible);
		break;
	case kGhostComponent_RightHand:
		m_rightHand->setVisible(visible);
		break;
	}
}

void GhostObject::runStateDefault()
{
	m_body->setVisible(true);
	m_mouth->setVisible(false);
	m_eyes->setVisible(false);
	m_leftHand->setVisible(false);
	m_rightHand->setVisible(false);
}

void GhostObject::runStateStay()
{
	m_body->setVisible(true);
	m_mouth->setVisible(true);
	m_eyes->setVisible(true);
	m_leftHand->setVisible(true);
	m_rightHand->setVisible(true);
}

void GhostObject::runStateYeah()
{
	m_body->setVisible(true);
	m_mouth->setVisible(true);
	m_eyes->setVisible(true);
	m_leftHand->setVisible(true);
	m_rightHand->setVisible(true);

	m_leftHand->runAction(RepeatForever::create(Sequence::create(RotateBy::create(0.1f, 45), RotateBy::create(0.2f, -45), NULL)));
	m_rightHand->runAction(RepeatForever::create(Sequence::create(RotateBy::create(0.1f, -45), RotateBy::create(0.2f, 45), NULL)));
}